<template>
  <div class="page-container">
    <!-- 背景装饰元素 -->
    <div class="background-decoration top-left"></div>
    <div class="background-decoration bottom-right"></div>
    <div class="floating-elements"></div>

    <!-- 半透明内容卡片 -->
    <div class="content-card">
      <h2>个人资料</h2>

      <el-form label-width="120px" class="info-form">
        <!-- 头像上传区域 -->
        <el-form-item label="头像">
          <div class="avatar-container">
            <el-avatar
                :size="80"
                :src="user.avatar ? '/img/' + user.avatar : defaultAvatar"
                class="avatar-preview"
            />
            <el-upload
                class="avatar-uploader"
                action="/api/user/file/wanggou"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <template #tip>
                <div class="el-upload__tip">
                  只能上传jpg/png文件，且不超过2MB
                </div>
              </template>
            </el-upload>
          </div>
        </el-form-item>

        <el-form-item label="昵称">
          <el-input v-model="user.nc" placeholder="请填写您的昵称" style="width: 300px"></el-input>
        </el-form-item>

        <el-form-item label="性别">
          <el-radio-group v-model="user.sex">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
            <el-radio label="保密">保密</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="saveUserInfo">保存</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { AuthService } from '@/service/auth.ts';
import axios from 'axios';
import { ElMessage, ElForm } from 'element-plus';

const authService = new AuthService();
const addressFormRef = ref(ElForm);

// 默认头像
const defaultAvatar = 'https://cube.elemecdn.com/3/7c/3ea6beec6434a26a67204e6b998f665/img/user.png';

const user = ref({
  nickname: '',
  nc: '',
  sex: '保密',
  avatar: '',
  birthday: '',
  intro: ''
});

const userId = authService.getUserId();

const getUserInfo = async () => {
  try {
    const { data } = await axios.get(`/api/user/getUserById/${userId}`);
    user.value = { ...user.value, ...data };
    console.log(data);
  } catch (error) {
    ElMessage.error('获取用户信息失败');
  }
};

const handleAvatarSuccess = (response) => {
  user.value.avatar = response;
  ElMessage.success('头像上传成功');
};

const beforeAvatarUpload = (rawFile) => {
  if (!['image/jpeg', 'image/png'].includes(rawFile.type)) {
    ElMessage.error('头像图片只能是 JPG 或 PNG 格式!');
    return false;
  }
  if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('头像图片大小不能超过 2MB!');
    return false;
  }
  return true;
};

const saveUserInfo = async () => {
  try {
    await axios.put(`/api/user`, user.value);
    ElMessage.success('保存成功');
  } catch (error) {
    ElMessage.error('保存失败');
  }
};

const resetForm = () => {
  getUserInfo();
  ElMessage.info('已重置为原始数据');
};

onMounted(getUserInfo);
</script>

<style scoped>
/* 动态背景效果 - 改为浅色渐变 */
.page-container {
  min-height: 100vh;
  padding: 40px 0;
  /* 浅色渐变背景 */
  background: linear-gradient(135deg, #f5f7fa, #eef2f7, #f9fbfd);
  background-size: 400% 400%;
  animation: gradientBG 15s ease infinite;
  position: relative;
  overflow: hidden;
}

@keyframes gradientBG {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

/* 浮动装饰元素 - 调整透明度和颜色 */
.floating-elements {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.floating-elements::before,
.floating-elements::after,
.page-container::before {
  content: '';
  position: absolute;
  border-radius: 50%;
  /* 更浅的背景色 */
  background: rgba(100, 150, 200, 0.05);
  animation: float 15s infinite ease-in-out;
}

.floating-elements::before {
  width: 200px;
  height: 200px;
  top: 20%;
  left: 10%;
  animation-delay: 0s;
}

.floating-elements::after {
  width: 150px;
  height: 150px;
  top: 60%;
  left: 80%;
  animation-delay: 5s;
}

.page-container::before {
  width: 100px;
  height: 100px;
  top: 40%;
  left: 60%;
  animation-delay: 2s;
}

@keyframes float {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  50% { transform: translateY(-30px) rotate(10deg); }
}

/* 增强版装饰背景 - 改为柔和色调 */
.background-decoration {
  position: absolute;
  width: 400px;
  height: 400px;
  border-radius: 50%;
  filter: blur(80px);
  z-index: 1;
}

/* 浅蓝色柔和装饰 */
.top-left {
  top: -150px;
  left: -150px;
  background: linear-gradient(135deg, #e6f2ff, #d9ecff);
  animation: pulse 8s infinite;
}

/* 浅粉色柔和装饰 */
.bottom-right {
  bottom: -200px;
  right: -200px;
  background: linear-gradient(135deg, #fff0e6, #ffe6d9);
  animation: pulse 10s infinite reverse;
}

@keyframes pulse {
  0%, 100% { opacity: 0.3; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(1.1); }
}

/* 内容卡片增强样式 - 略微调整以适应浅色背景 */
.content-card {
  width: 90%;
  max-width: 800px;
  margin: 0 auto;
  padding: 30px;
  background: rgba(255, 255, 255, 0.98);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(240, 240, 240, 0.8);
  position: relative;
  z-index: 2;
  overflow: hidden;
}

.content-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 5px;
  /* 调整顶部线条为柔和色调 */
  background: linear-gradient(90deg, #99ccff, #b3e0ff, #cce5ff, #e6f2ff);
  background-size: 200% 100%;
  animation: gradientLine 8s infinite;
}

@keyframes gradientLine {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

.content-card h2 {
  color: #303133;
  font-size: 24px;
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
  position: relative;
}

.content-card h2::after {
  content: '';
  position: absolute;
  width: 60px;
  height: 3px;
  background: #99ccff;
  bottom: -1px;
  left: 0;
}

/* 表单元素增强 */
.info-form {
  margin-top: 20px;
}

/* 头像区域特效 */
.avatar-container {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 0;
}

.avatar-preview {
  border: 3px solid transparent;
  border-radius: 50%;
  background: linear-gradient(white, white) padding-box,
  linear-gradient(135deg, #99ccff, #b3e0ff) border-box;
  transition: all 0.3s ease;
  position: relative;
}

.avatar-preview::after {
  content: '';
  position: absolute;
  top: -5px;
  left: -5px;
  right: -5px;
  bottom: -5px;
  border-radius: 50%;
  border: 1px dashed rgba(153, 204, 255, 0);
  transition: all 0.3s ease;
}

.avatar-preview:hover {
  transform: scale(1.05) rotate(5deg);
}

.avatar-preview:hover::after {
  border-color: rgba(153, 204, 255, 0.5);
  animation: rotate 8s linear infinite;
}

@keyframes rotate {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 按钮样式强化 */
::v-deep .el-button {
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

::v-deep .el-button::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  transition: width 0.6s ease, height 0.6s ease;
}

::v-deep .el-button:hover::after {
  width: 200px;
  height: 200px;
}

::v-deep .el-button + .el-button {
  margin-left: 15px;
}

/* 单选框美化 */
::v-deep .el-radio-group {
  display: flex;
  gap: 20px;
  margin-top: 5px;
}

::v-deep .el-radio {
  position: relative;
  padding-left: 25px;
  cursor: pointer;
}

::v-deep .el-radio__input.is-checked .el-radio__inner {
  border-color: #99ccff;
  background-color: #99ccff;
  box-shadow: 0 0 0 3px rgba(153, 204, 255, 0.2);
}

/* 输入框焦点效果 */
::v-deep .el-input__wrapper:focus-within {
  box-shadow: 0 0 0 2px rgba(153, 204, 255, 0.2);
  border-color: #99ccff;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .content-card {
    padding: 20px;
    width: 95%;
  }

  .background-decoration {
    width: 300px;
    height: 300px;
  }
}
</style>